package com.ucomm.talent.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.IFunctionViewListener;

/**
 * Function 按钮自定义类
 * Created with IntelliJ IDEA.
 * User: Vita Hao
 * Date: 13-5-22
 * Time: 上午10:23
 * To change this template use File | Settings | File Templates.
 */
public class FunctionView extends LinearLayout implements View.OnClickListener{
    private Drawable img;
    private String text;
    private View layout;
    private ImageView iv;
    private IFunctionViewListener viewListener;
    private int parentId;
    public FunctionView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.functionButton);
        this.text = a.getString(R.styleable.functionButton_functionIitle);
        this.img = a.getDrawable(R.styleable.functionButton_functionImg);

        layout = LayoutInflater.from(context).inflate(R.layout.gv_function_item, null);
        iv = (ImageView) layout.findViewById(R.id.gv_item_img);
        iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
        TextView tv = (TextView) layout.findViewById(R.id.gv_item_text);
        iv.setImageDrawable(img);
        iv.setClickable(true);
        iv.setOnClickListener(this);
        tv.setText(text);
        // LinearLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        this.setGravity(Gravity.CENTER);
        this.addView(layout);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean performClick() {
        return super.performClick();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void onClick(View view) {
        if(iv.getId() == view.getId()){
            viewListener.OnImgViewClickListener(parentId,view);
        }
    }

    public void setViewListener(int id ,IFunctionViewListener viewListener) {
        this.parentId = id;
        this.viewListener = viewListener;
    }
}
